package com.ciquan.server.controller;

import com.ciquan.server.bean.AdminInfo;
import com.ciquan.server.service.AdminInfoService;
import com.ciquan.server.util.MD5Utils;
import com.ciquan.server.util.web.MenuUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by bjliwei2014 on 2015/2/27.
 */
@Controller
@RequestMapping
public class AdminController extends AbstractController {

    @Resource
    private AdminInfoService adminInfoService;

    @RequestMapping("/")
    public ModelAndView index() {
        return new ModelAndView("login");
    }

    @RequestMapping("/login")
    public ModelAndView login(HttpServletRequest request, ModelMap modelMap) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        password = MD5Utils.md5(password).toLowerCase();
        AdminInfo adminInfo = adminInfoService.login(username, password);
        if(adminInfo == null) {
            modelMap.put("error", "用户名或者密码错误");
            return new ModelAndView("login", modelMap);
        }
        request.getSession().setAttribute("user", adminInfo);
        return new ModelAndView("redirect:/"+ MenuUtils.ADMIN_MENUS.get(0).getPath());
    }
}
